package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZLCfgGenerateDtlMapper;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlAddDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlDelDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlDetailDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlEditDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlQueryDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ReqCfgGenerateDtlStateDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlAddDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlDelDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlDetailDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlEditDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlQueryDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlStateDTO;
import com.zl.autocode.dto.autocode.cfgGenerateDtl.ResCfgGenerateDtlVO2;
import com.zl.autocode.entity.autocode.ZLCfgGenerateDtl;
import com.zl.autocode.entity.autocode.po.ZLCfgGenerateDtlVo;
import com.zl.autocode.service.autocode.ICfgGenerateDtlService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 代码生成配置明细
 * @author ZL
 */
@Service("autocodeCfgGenerateDtlService")
public class CfgGenerateDtlServiceImpl extends AutocodeBaseManager implements ICfgGenerateDtlService {

    @Autowired
    private ZLCfgGenerateDtlMapper cfgGenerateDtlMapper;

    @Override
    public ResCfgGenerateDtlQueryDTO queryCfgGenerateDtlList(ReqDTO<ReqCfgGenerateDtlQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResCfgGenerateDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResCfgGenerateDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getModuleKeyName())){
            reqData.setModuleKeyName("%"+reqData.getModuleKeyName()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getModuleKey())){
            reqData.setModuleKey("%"+reqData.getModuleKey()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZLCfgGenerateDtlVo> list = cfgGenerateDtlMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResCfgGenerateDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResCfgGenerateDtlDetailDTO queryCfgGenerateDtlDetail(ReqDTO<ReqCfgGenerateDtlDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlDetailDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDtlDetailDTO res = new ResCfgGenerateDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZLCfgGenerateDtl view = cfgGenerateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResCfgGenerateDtlAddDTO insertCfgGenerateDtlInfo(ReqDTO<ReqCfgGenerateDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlAddDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDtlAddDTO res = new ResCfgGenerateDtlAddDTO();
        Date time = getSystemTime().getDate();
        
        ZLCfgGenerateDtl record = new ZLCfgGenerateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setCfgGenerateDtlId(IdUtil.simpleUUID());
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateDtlId",record.getCfgGenerateDtlId());
        //检查主键是否冲突
        if(cfgGenerateDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        cfgGenerateDtlMapper.insertSelective(record);
        
        ZLCfgGenerateDtl keyInfo = cfgGenerateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateDtlEditDTO updateCfgGenerateDtlInfo(ReqDTO<ReqCfgGenerateDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlEditDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDtlEditDTO res = new ResCfgGenerateDtlEditDTO();
        Date time = getSystemTime().getDate();
        
        ZLCfgGenerateDtl record = new ZLCfgGenerateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        cfgGenerateDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateDtlId",record.getCfgGenerateDtlId());
        ZLCfgGenerateDtl keyInfo = cfgGenerateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateDtlDelDTO deleteCfgGenerateDtlInfo(ReqDTO<ReqCfgGenerateDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlDelDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDtlDelDTO res = new ResCfgGenerateDtlDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        cfgGenerateDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResCfgGenerateDtlStateDTO updateCfgGenerateDtlState(ReqDTO<ReqCfgGenerateDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqCfgGenerateDtlStateDTO reqData = reqInfo.getReqData();
        ResCfgGenerateDtlStateDTO res = new ResCfgGenerateDtlStateDTO();
        Date time = getSystemTime().getDate();
         
        ZLCfgGenerateDtl record = new ZLCfgGenerateDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        cfgGenerateDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("cfgGenerateDtlId",record.getCfgGenerateDtlId());
        ZLCfgGenerateDtl keyInfo = cfgGenerateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}